//
//  AlbumView.m
//  AlbumDemo
//
//  Created by hejinhui on 2018/1/22.
//  Copyright © 2018年 hejinhui. All rights reserved.
//

#import "AlbumView.h"
#import "UIView+Extension.h"
#import "AlbumItem.h"
#import "AlbumModel.h"
#define margin 10.f
#define ScreenWidth      [UIScreen mainScreen].bounds.size.width
#define ScreenHeight     [UIScreen mainScreen].bounds.size.height

@interface AlbumView()
{
    NSInteger _number;//总数
    NSInteger _columns;//列数
}
@end

@implementation AlbumView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
            
    }
    return self;
    
}

-(void)setAlbumDelegate:(id<AlbumDelegate>)albumDelegate
{
    _albumDelegate = albumDelegate;
    [self loadData];
}

-(void)loadData
{
    //行数
    if (_albumDelegate && [_albumDelegate respondsToSelector:@selector(numberOfItemView)]) {
        
        _number = [_albumDelegate numberOfItemView];
    }else
    {
        _number = 0;
    }
    //列数
    if (_albumDelegate && [_albumDelegate respondsToSelector:@selector(numberOfColumn)]) {
        _columns = [_albumDelegate numberOfColumn];
    }else{
     
        _columns = 0;
    }
    //创建
    [self initWithAblumView:_number columns:_columns];
    
    NSLog(@"---total-%ld--columns--%ld",_number,_columns);

}

/**
 numbers:显示的总个数
 column: 总列数
 */

-(void)initWithAblumView:(NSInteger)numbers columns:(NSInteger)columns
{
        //宽度
        CGFloat width = (ScreenWidth - margin*(columns+1))/columns;
        //高度
        CGFloat height = 534*width/834+30;
        
        for (NSInteger i = 0; i < numbers; i ++) {
            
            //列数
            NSInteger column = i % columns;
            //行数
            NSInteger rows = i / columns;
            
            //初始化view
            AlbumItem *contentView = [[AlbumItem alloc]init];
            CGRect rect = CGRectMake(margin + (width + margin)*column, margin+(height + margin) * rows, width, height);
            contentView.frame = rect;
            contentView.tag = i;
            [self addSubview:contentView];

            contentView.itemFrame = rect;
            
            //return cell
            NSIndexPath *indePath = [NSIndexPath indexPathForRow:i inSection:0];

            if (self.albumDelegate && [self.albumDelegate respondsToSelector:@selector(itemViewForAlbumView:itemForItemAtIndexPath:)]) {
                [self.albumDelegate itemViewForAlbumView:self itemForItemAtIndexPath:indePath];
            }
            
            //添加手势
            UITapGestureRecognizer *guster = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gusterTap:)];
            [contentView addGestureRecognizer:guster];
            
            //要是有btn，这就是btn的点击事件的回调
            contentView.itemBlock = ^(AlbumItem *itemView) {
                
                if (self.albumItemBlock) {
                    self.albumItemBlock(itemView);
                }
            };
            
        }
}


-(void)albumReload
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        
        
    }];
    
}
//tapRegesture click
-(void)gusterTap:(UITapGestureRecognizer *)tap
{
    //block 和 代理任选其一
    NSInteger tag = tap.view.tag;
    AlbumItem *itemV = (AlbumItem *)tap.view;
    if (self.albumDelegate && [self.albumDelegate respondsToSelector:@selector(itemViewDidSelect:itemView:)]) {
        [self.albumDelegate itemViewDidSelect:tag itemView:itemV];
    }
}


//返回尺寸

+(CGRect)getAlbumHeight:(int)numbers column:(int)colunm
{
   
    int row =  (numbers + colunm - 1)/colunm ;
    CGFloat width = (ScreenWidth - margin*(colunm+1))/colunm;
    CGFloat height = (534*width/834+30 + margin)*row + 10;
    return CGRectMake(0, 0, ScreenWidth, height);
}

@end
